package com.e.ticket;

import android.animation.ObjectAnimator;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;


public class Index extends Fragment{
    int mYear,mMonth,mDay,mWeek;
    private Button bt_select;
    private TextView tv_1,tv_2,date_1,date_2;
    private ImageView iv_1;
    private int count=0;
    private DatePicker datePicker;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = LayoutInflater.from(this.getActivity()).inflate(R.layout.fragment_index, null);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        tv_1 = (TextView) getActivity().findViewById(R.id.train_1);
        tv_2 = (TextView) getActivity().findViewById(R.id.train_2);
        iv_1 = (ImageView) getActivity().findViewById(R.id.qiehuan);
        date_1 = (TextView)getActivity().findViewById(R.id.date_1);
        date_2 = (TextView)getActivity().findViewById(R.id.date_2);
        bt_select = (Button)getActivity().findViewById(R.id.select_bt);
        datePicker = (DatePicker) getActivity().findViewById(R.id.datePicker);
        final Calendar calendar = Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR);
        mDay = calendar.get(Calendar.DAY_OF_MONTH);
        mMonth =calendar.get(Calendar.MONTH);
        mWeek = calendar.get(Calendar.DAY_OF_WEEK);
        initDate(mYear,mMonth,mDay,mWeek);
        date_1.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View view) {
                DatePickerDialog dialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                        mYear = i;
                        mMonth = i1;
                        mDay = i2;
                        mWeek = SetWeek(mYear,mMonth+1,mDay);
                        initDate(mYear,mMonth,mDay,mWeek);
                    }
                }, mYear,mMonth,mDay);
                dialog.getDatePicker().setMinDate(System.currentTimeMillis());
                dialog.show();
            }
        });

        date_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerDialog dialog1 = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                        mYear = i;
                        mMonth = i1;
                        mDay = i2;
                        mWeek = SetWeek(mYear,mMonth+1,mDay);
                        initDate(mYear,mMonth,mDay,mWeek);
                    }
                }, mYear,mMonth,mDay);
                dialog1.getDatePicker().setMinDate(System.currentTimeMillis());
                dialog1.show();
            }
        });
        iv_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                qiehuan(view);
            }
        });



        bt_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getActivity(),Tickes_Item.class);
                Bundle b = new Bundle();
                if(count % 2 ==0)
                    b.putInt("id",1);
                else
                    b.putInt("id",2);
                b.putInt("year",mYear);
                b.putInt("month",mMonth);
                b.putInt("days",mDay);
                b.putInt("week",mWeek);
                intent.putExtras(b);
                startActivity(intent);
            }
        });
    }

    public void qiehuan(View v){
        if(count % 2 == 0) {
            tv_1.setText("金堂校区");
            tv_2.setText("洪河校区");
        }
        else{
            String temp = tv_1.getText().toString();
            tv_1.setText(tv_2.getText().toString());
            tv_2.setText(temp);

        }
        text_1();
        //设置切换按钮的动画
        final float centerX = v.getHeight() / 2.0f;
        final float centerY = v.getWidth() / 2.0f;
        //第一个是起始角度，第二个是动画结束角度，第三个是X轴，第四个是Y轴
        RotateAnimation rotateAnimation = new RotateAnimation(0, 180, centerX, centerY );
        rotateAnimation.setDuration(1000);
        //表示动画停留在最后位置
        rotateAnimation.setFillAfter(true);
        v.startAnimation(rotateAnimation);
        count++;
    }

    public void text_1(){
        //平移左边
        ObjectAnimator animator = ObjectAnimator.ofFloat(tv_1, "translationX", 0,600);
        animator.setDuration(1000);
        animator.start();

        //平移右边
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(tv_2, "translationX", 0,-600);
        animator1.setDuration(1000);
        animator1.start();

    }

    public void initDate(int year,int month,int day,int week){
        System.out.println(month+":"+day+":");
        String a = toWeek(week);
        date_1.setText(month+1+"月"+day+"日");
        date_2.setText("星期"+a);
    }
    public String toWeek(int day){
        String Temp = new String();
        if (day==2)
            Temp="一";
        else if (day==3)
            Temp="二";
        else if(day==4)
            Temp="三";
        else if (day==5)
            Temp="四";
        else if (day==6)
            Temp="五";
        else if (day==7)
            Temp="六";
        else if (day==8)
            Temp="日";
        return Temp;
    }



    //获取当前周week
    int SetWeek(int y,int m,int d){
        if(m==1||m==2) {
            m+=12;
            y--;
        }
        int iWeek=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
        return iWeek+2;
    }


}